#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>

#define NN 64

void* RecvData(void* arg)
{
    char buf[NN]={0};
    uint32_t len=0;
    int* c_socket=(int*)arg;
    int client_socket=*c_socket;
    free(c_socket);
    while (1)
    {
        len=recvfrom(client_socket,buf,64,0,NULL,NULL);
        if (len>0)
        {
            if (strcmp("login",buf)==0)
            {
                memset(buf,0,64);
                recvfrom(client_socket,buf,64,0,NULL,NULL);
                printf("%s用户加入了聊天室!\n",buf);
                continue;
            }

            if (strcmp("quit",buf)==0)
            {
                memset(buf,0,64);
                recvfrom(client_socket,buf,64,0,NULL,NULL);
                printf("%s用户退出了聊天室!\n",buf);
                continue;
            }
            printf("recv data = %s\n",buf);
        }
    }

    close(client_socket);
    return NULL;
}

int main(int argc, char const* argv[])
{
    int client_socket = socket(AF_INET,SOCK_DGRAM,0);
    if (client_socket==-1)
    {
        perror("socket err\n");
        return -1;
    }

    struct sockaddr_in server_addr;
    server_addr.sin_family=AF_INET;
    server_addr.sin_port=htons(8888);
    server_addr.sin_addr.s_addr=inet_addr(argv[1]);

    char buf[64]={'\0'};
    char tbuf[64]={'\0'};
    printf("请输入要登录的帐号:\n");
    scanf("%s",buf);

    sendto(client_socket,"login",5,0,(struct sockaddr*)&server_addr,sizeof(server_addr));
    ssize_t ret = sendto(client_socket,buf,strlen(buf),0,(struct sockaddr*)&server_addr,sizeof(server_addr));
    if(ret<0)
    {
        perror("sendto err\n");
        return -1;
    }

    ssize_t len=recvfrom(client_socket,tbuf,64,0,NULL,NULL);
    if (strcmp("login",tbuf)==0 && len>0)
    {
        recvfrom(client_socket,tbuf,64,0,NULL,NULL);
        if (strcmp(buf,tbuf)==0)
        {
            printf("登录成功\n");
        }
    }
    else
    {
        printf("登录失败,请重新登入!\n");
        return -1;
    }

    while (1)
    {
        memset(buf,0,64);
        printf("在此输入信息:(最多64个字节)\n");
        scanf("%s",buf);

        ret = sendto(client_socket,buf,strlen(buf),0,(struct sockaddr*)&server_addr,sizeof(server_addr));
        if(ret<0)
        {
            perror("sendto err\n");
            return -1;
        }
        if (strcmp("quit",buf)==0)
        {
            sendto(client_socket,tbuf,strlen(tbuf),0,(struct sockaddr*)&server_addr,sizeof(server_addr));
            printf("退出帐号!\n");
            break;
        }
        
        pthread_t thread;
        int *c_socket=(int*)malloc(sizeof(int));
        *c_socket=client_socket;
        if(pthread_create(&thread,NULL,RecvData,(void*)c_socket) != 0)
        {
            perror("create thread error\n");
            close(client_socket);
            return -1;
        }
        pthread_detach(thread);
        
    }
        
    close(client_socket);

    return 0;
}